/**
 * *****************************************************************************
 * This file is part of the EEG-database project
 *
 * ==========================================
 *
 * Copyright (C) 2013 by University of West Bohemia (http://www.zcu.cz/en/)
 *
 *  ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 *  ***********************************************************************************************************************
 *
 * Article.java, 2013/10/02 00:01 Jakub Rinkes
 * ****************************************************************************
 */
package cz.zcu.kiv.eegdatabase.data.pojo;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

import cz.zcu.kiv.eegdatabase.data.annotation.Indexed;
import cz.zcu.kiv.eegdatabase.data.annotation.SolrField;
import cz.zcu.kiv.eegdatabase.data.annotation.SolrId;
import cz.zcu.kiv.eegdatabase.logic.indexing.IndexField;


/**
 * Article generated by hbm2java
 */
@Entity
@Indexed
@Table(name = "ARTICLES")
public class Article implements Serializable {

	@SolrId
	private int articleId;
	private Person person;
	private ResearchGroup researchGroup;
	private Timestamp time;
	@SolrField(name = IndexField.TEXT)
	private String text;
	@SolrField(name = IndexField.TITLE)
	private String title;
	private Set<ArticleComment> articleComments = new HashSet<ArticleComment>(0);
	private Set<Person> subscribers = new HashSet<Person>(0);

	public Article() {
	}

	public Article(Person person) {
		this.person = person;
	}

	public Article(Person person, ResearchGroup researchGroup, Timestamp time,
			String text, String title, Set<ArticleComment> articleComments,
			Set<Person> subscribers) {
		this.person = person;
		this.researchGroup = researchGroup;
		this.time = time;
		this.text = text;
		this.title = title;
		this.articleComments = articleComments;
		this.subscribers = subscribers;
	}

	@GenericGenerator(name = "generator", strategy = "increment")
	@Id
	@GeneratedValue(generator = "generator")
	@Column(name = "ARTICLE_ID", nullable = false, precision = 22, scale = 0)
	public int getArticleId() {
		return this.articleId;
	}

	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "PERSON_ID", nullable = false)
	public Person getPerson() {
		return this.person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "RESEARCH_GROUP_ID")
	public ResearchGroup getResearchGroup() {
		return this.researchGroup;
	}

	public void setResearchGroup(ResearchGroup researchGroup) {
		this.researchGroup = researchGroup;
	}

	@Column(name = "TIME", length = 7)
	public Timestamp getTime() {
		return this.time;
	}

	public void setTime(Timestamp time) {
		this.time = time;
	}

	@Lob
	@Type(type = "org.hibernate.type.TextType")
	@Column(name = "TEXT")
	public String getText() {
		return this.text;
	}

	public void setText(String text) {
		this.text = text;
	}

	@Column(name = "TITLE", nullable = false)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "article")
	@OrderBy("time ASC")
	public Set<ArticleComment> getArticleComments() {
		return this.articleComments;
	}

	public void setArticleComments(Set<ArticleComment> articleComments) {
		this.articleComments = articleComments;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "ARTICLES_SUBSCRIBTIONS", joinColumns = {
		@JoinColumn(name = "ARTICLE_ID", nullable = false, updatable = false)}, inverseJoinColumns = {
		@JoinColumn(name = "PERSON_ID", nullable = false, updatable = false)})
	public Set<Person> getSubscribers() {
		return this.subscribers;
	}

	public void setSubscribers(Set<Person> subscribers) {
		this.subscribers = subscribers;
	}
}
